local ST = LibStub("AceAddon-3.0"):GetAddon("SurgeTrack", true)
local STA = ST:NewModule("SurgeTrack_Aura")
local L = LibStub("AceLocale-3.0"):GetLocale(ST:GetName(), true)

local function auraIsInTable(spellId,aura_table)
 return type(aura_table[spellId]) == "boolean"
end

local function auraNeedTrack(spellId,aura_table)
 return aura_table[spellId] and aura_table[spellId].track
end

function ST:COMBAT_LOG_EVENT(event,timestamp,subEvent,x_arg0,sourceGUID,sourceName,sourceFlags,x_arg1,destGUID,destName,destFlags,x_arg2,spellId,spellName,x_arg3,auraType,amount)

 if destGUID ~= UnitGUID("player") or not spellId or (auraType ~= "BUFF" and auraType ~= "DEBUFF") then return end
 if ST.GATHER then
  -- ST:Print(L["G_GUIDE_DISABLE_GATHER_MODE_CMD"])
  if auraIsInTable(spellId,ST.db.profile.aura.class) or auraIsInTable(spellId,ST.db.profile.aura.common) or auraIsInTable(spellId,ST.db.profile.aura.custom) then return end
  ST:addCustomSpellToDb(spellId)
 else
  if not auraNeedTrack(spellId,ST.aura) then return end
  local mask
  --how mask works:
  --containerId not exist
  --spellId spellName spellIcon auraType change
  --event change
  --subEvent change
  --timestamp duration change
  --amount change
  local commit_info = {event = event, subEvent = subEvent, timestamp = timestamp, spellId = spallId, spellName = spellName, auraType = auraType}
  if not ST.active_item[spellId] then
   ST.active_item[spellId] = {}
   mask = tonumber("111111",2)
  end
  if subEvent == "SPELL_AURA_APPLIED" or subEvent == "SPELL_AURA_REFRESH" or subEvent == "SPELL_AURA_APPLIED_DOSE" or subEvent == "SPELL_AURA_REMOVED_DOSE" then
   mask = mask or tonumber("111000",2)
     
   ST:updateItem(spellId, commit_info, mask)
  elseif subEvent == "SPELL_AURA_REMOVED" or subEvent == "SPELL_AURA_BROKEN" then
   ST:releaseItem(spellId)
  end
 end
end

function ST:UNIT_AURA(event, unit)
 if unit ~= "player" then return end
 for spellId,activeItemInfo in pairs(ST.active_item) do
  if not spellId or not auraNeedTrack(spellId,ST.aura) then return end
  --name, rank, icon, count, debuffType, duration, expirationTime, unitCaster, isStealable, shouldConsolidate, spellId
  local spellName = select(1, GetSpellInfo(spellId))
  local amount = select(4, UnitBuff("player", spellName)) or select(4, UnitDebuff("player", spellName)) or 0
  local duration = select(6, UnitBuff("player", spellName)) or select(6, UnitDebuff("player", spellName)) or 0
  local expires = select(7, UnitBuff("player", spellName)) or select(7, UnitDebuff("player", spellName)) or 0
  local timestamp = expires-duration
  commit_info = {event = event, timestamp = timestamp, spellId = spallId, spellName = spellName, amount = amount, duration = duration}
  mask = mask or tonumber("110100",2)
  ST:updateItem(spellId, commit_info, mask)
 end
end

function STA:GatherMode(enable)
 ST.GATHER = enable
 if enable then
  ST:Print(L["G_CONSOLE_STATUS_GATHER_MODE_ENABLE"] .. L["G_CONSOLE_INFO_GATHER_MODE_WORKING"])
  -- ST:Print(L["G_GUIDE_DISABLE_GATHER_MODE_CMD"])
  --local msg = L["G_GUIDE_AURA_ADD"]
  --message(msg)
  StaticPopup_Show("GatherModeDialog")
  ST:DeleteContainer(1 , ST.db.profile.unit.amount)
  LibStub("AceConfigDialog-3.0"):Close(ST.NAME)
 else
  -- ST:Print(L["G_CONSOLE_STATUS_GATHER_MODE_DISABLE"])
  StaticPopup_Hide("GatherModeDialog")
  LibStub("AceConfigRegistry-3.0"):NotifyChange(ST.NAME)
  LibStub("AceConfigDialog-3.0"):Open(ST.NAME)
 end
end

function ST:addCustomSpellToDb(spellId)
 ST.db.profile.aura.custom[spellId] = false
 ST:Print(L["O_H_AURA_ADD"]..": "..select(1,GetSpellLink(spellId)))
 LibStub("AceConfigRegistry-3.0"):NotifyChange(ST.NAME)
end
